local MeleeBuff = MClass("MeleeBuff")
local battleCfg = battle.BattleConfig:getInstance()
local newFloatTipsView = require("app.newFloatTips.newFloatTipsView")

function MeleeBuff:ctor()
    --self.root 默认为根节点
    --设置事件
    self.goldNum = self:findChild("Panel/btn_gold/num")
    self.soulNum = self:findChild("Panel/btn_soul/num")
	self.goldBtn = self:findChild("Panel/btn_gold")
	self.soulBtn = self:findChild("Panel/btn_soul")
    self.btn1 = self:findChild("Panel/btn_1")
    self.btn2 = self:findChild("Panel/btn_2")
    self.btn3 = self:findChild("Panel/btn_3")

    self.btn1:setTag(1)
    self.btn2:setTag(2)
    self.btn3:setTag(3)

    self.layout = {
    	{img =	self.btn1:findChild("img"),icon = self.btn1:findChild("icon"),btn = self.btn1},
    	{img =	self.btn2:findChild("img"),icon = self.btn2:findChild("icon"),btn = self.btn2},
    	{img =	self.btn3:findChild("img"),icon = self.btn3:findChild("icon"),btn = self.btn3},
	}


    self:init()
end

function MeleeBuff:init()
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.MELEE_PROP,handler(self,MeleeBuff.setProp))
	self.btn1:setTouchEnabled(true)
	self.btn2:setTouchEnabled(true)
	self.btn3:setTouchEnabled(true)
	self.goldBtn:setTouchEnabled(true)
	self.soulBtn:setTouchEnabled(true)
	local function touchBuff(sender, eventType)
		local tag = sender:getTag()
        if eventType == ccui.TouchEventType.ended then
        	if self.layout[tag].state then
        		--todo
        	else
        		BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SHOW_BATTLE_SHOP, 2)
        	end
        end
	end
    self.btn1:addTouchEventListener(touchBuff)
    self.btn2:addTouchEventListener(touchBuff)
    self.btn3:addTouchEventListener(touchBuff)
	local function clickCallBackGold(sender, eventType)
		if eventType == ccui.TouchEventType.ended then
			BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SHOW_BATTLE_SHOP, 1)
		end
	end
	self.goldBtn:addTouchEventListener(clickCallBackGold)
	local function clickCallBackSoul(sender, eventType)
		if eventType == ccui.TouchEventType.began then
			local param =
			{
				tipsType = Enums.TipsType.desc,
				title = L"soulpower_title",
				content = L"soulpower_tips",
			}
			app:dispatchCustomEvent("EVENT_OPEN_FLOAT", { params=param })
		elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
			app:dispatchCustomEvent("EVENT_CLOSE_FLOAT")
		end
	end
	self.soulBtn:addTouchEventListener(clickCallBackSoul)
end

function MeleeBuff:setProp(money, buffs, skills, soul, unit)
	self.goldNum:setString(money)
	self.soulNum:setString(soul)
	self.num = #buffs
	for i = 1,3 do
		if i <= self.num then
			self.layout[i].img:setVisible(false)
			self.layout[i].icon:setVisible(true)
			self.cfg = battleCfg:getItemConfig(unit:getItemIdByInstanceID(buffs[i]))
			--local price = self.cfg :getItemPrice()
			self.iconPic = self.cfg :getItemIcon()
			--local name = L(self.cfg :getItemName())
			self.layout[i].icon:loadTexture(string.format(Res.meleeBuffIconPath, self.iconPic))
			self.layout[i].state = true
		else
			self.layout[i].img:setVisible(true)
			self.layout[i].icon:setVisible(false)
			self.layout[i].state = false
		end
	end

end

return MeleeBuff